//
//  UserAbstractBaseView.m
//  LXFCommon
//
//  Created by lixfey on 2019/3/19.
//  Copyright © 2019 lixfey. All rights reserved.
//

#import "UserAbstractBaseView.h"

@interface AbstractBaseModel ()

@end

@implementation AbstractBaseModel

@end

@interface UserAbstractBaseView ()

@property(nonatomic, strong) UIImageView *imageView;
@property(nonatomic, strong) UIImageView *sexImageView;
@property(nonatomic, strong) UIImageView *iconImageView;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UILabel *detailLabel;

@end

@implementation UserAbstractBaseView

- (void)addSubviews {
    [self addSubview:self.imageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.sexImageView];
    [self addSubview:self.iconImageView];
    [self addSubview:self.detailLabel];

    self.imageView.tag = 1;
    self.sexImageView.tag = 3;
    self.titleLabel.tag = 2;
    self.detailLabel.tag = 4;

    self.imageView.layer.masksToBounds = true;
    self.imageView.layer.cornerRadius = 14;

    self.titleLabel.font = [UIFont systemFontOfSize:12];
    self.titleLabel.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];


    //TODO:
    self.iconImageView.backgroundColor = [UIColor cyanColor];

    self.detailLabel.font = [UIFont systemFontOfSize:10];
    self.detailLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];

    [self addConstraints];
    [self addGestureRecognizers];
}

- (void)addGestureRecognizers {
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickView:)]];

    for (UIView *view in self.subviews) {
        view.userInteractionEnabled = true;
        [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickView:)]];
    }
}

- (void)clickView:(UITapGestureRecognizer *)gester {
    if (self.AbstractBaseViewClickBlock) {
             self.AbstractBaseViewClickBlock(gester.view.tag);
    }
}

- (void)addConstraints {
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(11);
        make.centerY.offset(0);
        make.width.height.offset(28);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView.mas_right).offset(7);
        make.top.equalTo(self.imageView.mas_top);
        make.height.offset(12);
    }];
    [self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(4);
        make.centerY.equalTo(self.titleLabel);
        make.width.height.offset(10);
    }];

    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.detailLabel);
        make.width.offset(6);
        make.height.offset(8);
        make.right.equalTo(self.detailLabel.mas_left);
    }];

    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView.mas_right).offset(15);
//        make.top.equalTo(self.titleLabel.mas_bottom);
        make.bottom.equalTo(self.imageView.mas_bottom).offset(-3);
        make.height.offset(10);
    }];
}

- (void)setModel:(AbstractBaseModel *)model {
    _model = model;

    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.picUrl] placeholderImage:nil];
    self.titleLabel.text = model.name;
    self.detailLabel.text = model.intro;

    //TODO:
    switch (model.sex) {
        case AbstractSexTypeMan:
            self.sexImageView.backgroundColor = UIColor.greenColor;
            break;
        case AbstractSexTypeWoMan:
            self.sexImageView.backgroundColor = UIColor.redColor;
            break;
        case AbstractSexTypeNone:
            self.sexImageView.backgroundColor = UIColor.blueColor;
            break;
    }

}



- (instancetype)init {
    self = [super init];
    if (self) {
        [self addSubviews];
    }

    return self;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
    }
    return _iconImageView;
}


- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [UIImageView new];
    }
    return _imageView;
}

- (UIImageView *)sexImageView {
    if (!_sexImageView) {
        _sexImageView = [UIImageView new];
    }
    return _sexImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
    }
    return _titleLabel;
}

- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [UILabel new];
    }
    return _detailLabel;
}


@end
